﻿using System;
using System.Runtime.Serialization;

namespace RRSimulator.BlobManager.Service.Responses
{
    [DataContract]
    [KnownType(typeof(DeleteFileResponse))]
    [KnownType(typeof(UploadBlockResponse))]
    [KnownType(typeof(CommitBlocksResponse))]
    [KnownType(typeof(UploadFileResponse))]
    public class BlobManagerResponseBase : IBlobManagerResponse, IDisposable
    {
        private bool _isDisposed;
        private bool _isError;

        [DataMember]
        public string BlobName { get; set; }

        [DataMember]
        public string ErrorMessage { get; set; }

        [DataMember]
        public bool IsError
        {
            get
            {
                return _isError || !string.IsNullOrEmpty(ErrorMessage);
            }
            set
            {
                _isError = value;
            }
        }

        [DataMember]
        public string ElapsedTime { get; set; }

        ~BlobManagerResponseBase()
        {
            Dispose(false);
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool isDisposing)
        {
            if (!_isDisposed)
            {
                if (isDisposing)
                {
                    BlobName = null;
                    ErrorMessage = null;
                    ElapsedTime = null;
                }

                _isDisposed = true;
            }
        }
    }    
}
